<?php

// class viewpoints
// id - identifier of viewpoint
// name - name of viewpoint
// x - xcoordinate
// y - ycoordinate
// z - zcoordinate
Class viewpoint_class{
    var $id,
        $name,
        $x,
        $y,
        $z;
};

// This function sort viewpoints by names
function sort_viewpoints(&$viewpoints){
    for ($i = 0; $i < count($viewpoints) - 1; $i++){
        for ($j = 0; $j < count($viewpoints) - 1; $j++){
            if (strcmp($viewpoints[$j]->name, $viewpoints[$j + 1]->name) > 0){
                $time = $viewpoints[$j]->name;
                $viewpoints[$j]->name = $viewpoints[$j+1]->name;
                $viewpoints[$j + 1]->name = $time;

                $time = $viewpoints[$j]->x;
                $viewpoints[$j]->x = $viewpoints[$j+1]->x;
                $viewpoints[$j + 1]->x = $time;

                $time = $viewpoints[$j]->y;
                $viewpoints[$j]->y = $viewpoints[$j+1]->y;
                $viewpoints[$j + 1]->y = $time;

                $time = $viewpoints[$j]->z;
                $viewpoints[$j]->z = $viewpoints[$j+1]->z;
                $viewpoints[$j + 1]->z = $time;

            };
        };
    };

};

// function for determine parameters for visualization (level_arr containse count of classes on each levels)
function determine_parameters($parent_id, $level, &$level_arr, $onto_id){
    $sql = "SELECT id FROM class WHERE parent_id = ".$parent_id." and onto_id = ".$onto_id;
    $i_max = getMultData($sql, $idclasses);
    if ($level_arr[$level] == '')
      $level_arr[$level] = $i_max;
    else
      $level_arr[$level] += $i_max;

    for ($i = 0; $i < $i_max; $i++)
      determine_parameters($idclasses[$i]["id"], $level + 1, &$level_arr, $onto_id);
};

function generate_ontology($onto_id, $x_c, $z_c, $y, $parent_class_position, $ThingClassID){
  /*$sql = "SELECT id FROM class where parent_id = 0 and onto_id = ".$onto_id;
  getData($sql, $ThingClassIDData);
  $ThingClassID = $ThingClassIDData["id"];*/

  determine_parameters($ThingClassID, 0, $level_arr, $onto_id);
  //Setting starting value for visualization
  $R = 0; // radius of class or attribute cicle
  for ($i = 0; $i < count($level_arr); $i++)
    $R += $level_arr[$i] * 2;
  $lod = ($R * 40); // distance between watcher and class (visibility of class)
  $deltaR = 2; $deltay = 10;

  //Recursive algorithm call for drawing ontology
  child_class($ThingClassID, 0, $parent_class_position, array(), array(), $acon, $compatibility, $hierarchy, $x_c, $z_c, $y, $R, $deltaR, $deltay, $lod, $output, $viewpoints);
  return $output;
};

// This is the main function its generate vrml code of taxonomy
function generation_code($onto_id, &$view, $ThingClassID){
    set_time_limit(9000);
    global $_GET;
    include_once 'drawontology.php';
    include_once 'vrml_shapes.php';

    $viewpoints = array();
    if ($ThingClassID == 0){
      if (is_numeric($onto_id)){
        // Ontology from main menu
        $sql = "SELECT c.id, t.text FROM class c, term t WHERE c.term_id = t.id and t.lang_id = 0 and c.parent_id = 0 and c.onto_id=".$onto_id;
        getData($sql, $class_id);
        $ThingClassID = $class_id["id"];
        $half_onto = 0;
      }else{
        // Application Ontology (o133c84174)
        die("This function not realised now");
      };
    }else{
      // Class
      $sql = "SELECT t.text FROM term t, class c WHERE c.term_id = t.id and t.lang_id = 0 and c.id=".$ThingClassID;
      getData($sql, $class_id);
      $half_onto = 1;
    };

    $sql = "SELECT DISTINCT c.base_onto_id, t.text FROM class c, ontology o, term t WHERE c.base_onto_id = o.id and o.term_id = t.id and t.lang_id = 0 and c.onto_id = ".$onto_id." ORDER BY c.base_onto_id";
    $i_max = getMultData($sql, $base_onto_id);

    if (($i_max == 0) or ($i_max == 1) or $half_onto == 1){
      // usual ontology

      $output = onto_Thing($class_id["text"]);
      $viewpoints[0]->id = "Thing1"; $viewpoints[0]->name = "Thing"; $viewpoints[0]->x = 0; $viewpoints[0]->y = 15; $viewpoints[0]->z = 20;
      $x_c = 0; $z_c = 0; $y = 10; $parent_class_position = $x_c.' '.'20'.' '.$z_c;
      $output .= generate_ontology($onto_id, $x_c, $z_c, $y, $parent_class_position, $ThingClassID);
    }else{
      // ontology with two ontologies (like application ontology)
      $output = onto_Thing("Thing");
      $viewpoints[0]->id = "Thing1"; $viewpoints[0]->name = "0 Thing"; $viewpoints[0]->x = 0; $viewpoints[0]->y = 15; $viewpoints[0]->z = 20;

      $x_c = 50; $z_c = 0; $y = 0; $y_c = 10; $parent_class_position = $x_c.' '.'10'.' '.$z_c;
      $output .= onto_Class("Ontology: ".$base_onto_id[0]["text"], "b".$base_onto_id[0]["base_onto_id"], $x_c, $y_c, $z_c);
      $output .= onto_relationship("0, 20, 0, ".$x_c.", ".$y_c.", ".$z_c, "ThingRel1", "1 1 1");
      $viewpoints[1]->id = "Thing1"; $viewpoints[1]->name = "Ontology: ".$base_onto_id[0]["text"]; $viewpoints[1]->x = $x_c; $viewpoints[1]->y = $y_c; $viewpoints[1]->z = $z_c;

      $output .= generate_ontology($base_onto_id[0]["base_onto_id"], $x_c, $z_c, $y, $parent_class_position, $ThingClassID);

      $x_c = -50; $z_c = 0; $y = 0; $y_c = 10; $parent_class_position = $x_c.' '.'10'.' '.$z_c;
      $output .= onto_Class("Ontology: ".$base_onto_id[1]["text"], "b".$base_onto_id[1]["base_onto_id"], $x_c, $y_c, $z_c);
      $output .= onto_relationship("0, 20, 0, ".$x_c.", ".$y_c.", ".$z_c, "ThingRel2", "1 1 1");
      $viewpoints[2]->id = "Thing1"; $viewpoints[2]->name = "Ontology: ".$base_onto_id[1]["text"]; $viewpoints[2]->x = $x_c; $viewpoints[2]->y = $y_c; $viewpoints[2]->z = $z_c;
      $output .= generate_ontology($base_onto_id[1]["base_onto_id"], $x_c, $z_c, $y, $parent_class_position, $ThingClassID);
    };

    //Output viewpoints
    sort_viewpoints($viewpoints);
    $output .= onto_viewpoints($viewpoints);

    //Output non-taxonomy relationships (acon, compatibility, hierarchy)
    if ((isset($_GET["acon"]))&&($_GET["acon"]==1))
      for ($i = 0; $i < count($acon); $i++)
            $output .= onto_relationship($acon[$i]->x1.", ".$acon[$i]->y1.", ".$acon[$i]->z1.", ".$acon[$i]->x2.", ".$acon[$i]->y2.", ".$acon[$i]->z2, str_replace(" ", "", $acon[$i]->name1.$acon[$i]->name2), "0.27 0.93 0.8");

    if ((isset($_GET["compability"]))&&($_GET["compability"]==1))
      for ($i = 0; $i < count($compatibility); $i++)
        $output .= onto_relationship($compatibility[$i]->x1.", ".$compatibility[$i]->y1.", ".$compatibility[$i]->z1.", ".$compatibility[$i]->x2.", ".$compatibility[$i]->y2.", ".$compatibility[$i]->z2, $compatibility[$i]->id, "0.91 0.18 0.98");

    if ((isset($_GET["hierarchy"]))&&($_GET["hierarchy"]==1))
      for ($j = 0; $j < count($hierarchy); $j++)
        $output .= onto_relationship($hierarchy[$j]->x1.", ".$hierarchy[$j]->y1.", ".$hierarchy[$j]->z1.", ".$hierarchy[$j]->x2.", ".$hierarchy[$j]->y2.", ".$hierarchy[$j]->z2, $hierarchy[0]->id, "0.58 0.05 0.99");

    set_time_limit(60);
    return $output;
};
?>